package com.iflytek.exam.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.iflytek.exam.domain.*;
import com.iflytek.exam.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Slf4j
public class BulkImportServiceImpl implements BulkImportService {

    @Autowired
    IChoiceService iChoiceService;

    @Autowired
    IJudgeService judgeService;

    @Autowired
    IShortAnswerService shortAnswerService;

    @Autowired
    ITbAllService tbAllServices;

    @Autowired
    ITbQuestionSummaryService questionSummaryService;


    //读取选择题
    @Override
    public void readExcelForChoice(String filePath, Long quBankId, Integer quTypeId, int sheetNum) {
        AtomicReference<Integer> num = new AtomicReference<>(0);
        AtomicReference<Long> singleNum = new AtomicReference<>(0L);
        AtomicReference<Long> multipleNum = new AtomicReference<>(0L);
        Pattern pattern = Pattern.compile("[ABCDEF]");
        EasyExcel.read(filePath, ExcelChoice.class, new PageReadListener<ExcelChoice>(questionList -> {
            for (ExcelChoice question : questionList) {
                //log.info("读取到一条数据{}", question);
                num.getAndSet(num.get() + 1);
                int type = 0;
                if (question.getType().contains("单选")) {
                    type = 1;
                } else if (question.getType().contains("多选")) {
                    type = 2;
                }
                TbAll tbAll = new TbAll();
                if (type == 1 || type == 2) {
                    new AtomicReference<>((type == 1) ? singleNum.getAndSet(singleNum.get() + 1) : multipleNum.getAndSet(multipleNum.get() + 1));
                    Choice choice = new Choice();
                    choice.setQuBankId(quBankId);
                    choice.setTitle(question.getTitle());
                    choice.setCategoryFirst(question.getCategoryFirst());
                    choice.setCategorySecond(question.getCategorySecond());
                    choice.setCategoryThrid(question.getCategoryThird());
                    choice.setType(type);
                    choice.setDegree(Integer.parseInt(question.getDegree()));
                    Matcher matcher = pattern.matcher(question.getAnswer());
                    StringBuilder extractedOptions = new StringBuilder();
                    while (matcher.find()) {
                        if (extractedOptions.length() > 0) {
                            extractedOptions.append(",");
                        }
                        extractedOptions.append(matcher.group());
                    }
                    //设置匹配规则之后的答案
                    choice.setAnswer(extractedOptions.toString());
                    choice.setAnswerAnalysis(question.getAnswerAnalysis());
                    choice.setOptionA(question.getOptionA());
                    choice.setOptionB(question.getOptionB());
                    choice.setOptionC(question.getOptionC());
                    choice.setOptionD(question.getOptionD());
                    choice.setOptionE(question.getOptionE());
                    choice.setOptionF(question.getOptionF());
                    //添加选择题到题库
                    iChoiceService.insertChoice(choice);
                    Choice choiceByName = iChoiceService.selectChoiceByName(choice.getTitle());
                    tbAll.setAllId(choiceByName.getId());
                    tbAll.setQuBankId(quBankId);
                    tbAll.setTitle(choiceByName.getTitle());
                    tbAll.setCategoryFirst(choiceByName.getCategoryFirst());
                    tbAll.setCategorySecond(choiceByName.getCategorySecond());
                    tbAll.setCategoryThrid(choiceByName.getCategoryThrid());
                    tbAll.setType(type);
                    tbAll.setDegree(choiceByName.getDegree());
                    //添加题目基本信息到题库和题目的中间表
                    tbAllServices.insertTbAll(tbAll);
                }
            }
            TbQuestionSummary questionSummary = new TbQuestionSummary();
            questionSummary.setId(quBankId);
            questionSummary.setSingleNum(singleNum.get());
            questionSummary.setMultipleNum(multipleNum.get());
            //更新tb_question_summer表中各种题的数量
            questionSummaryService.updateTbQuestionSummary(questionSummary);
            System.out.println("选择题读取到" + num.get() + "条数据");
            System.out.println(questionSummary);
        })).sheet(sheetNum).doRead();
    }
    public void readExcelForJudge(String filePath, Long quBankId, Integer quTypeId, int sheetNum) {
        AtomicReference<Long> judgeNum = new AtomicReference<>(0L);
        EasyExcel.read(filePath, ExcelJudge.class, new PageReadListener<ExcelJudge>(questionList -> {
            for (ExcelJudge question : questionList) {
                System.out.println(question);
                judgeNum.getAndSet(judgeNum.get() + 1);
                Judge judge = new Judge();
                judge.setQuBankId(quBankId);
                judge.setTitle(question.getTitle());
                judge.setCategoryFirst(question.getCategoryFirst());
                judge.setCategorySecond(question.getCategorySecond());
                judge.setCategoryThrid(question.getCategoryThird());
                judge.setDegree(Integer.parseInt(question.getDegree()));
                String answer = question.getAnswer();
                if (answer.contains("正确") || answer.contains("对") || answer.contains("T")) {
                    judge.setAnswer(1);
                } else if (answer.contains("错误") || answer.contains("错") || answer.contains("F")) {
                    judge.setAnswer(0);
                }
                judge.setAnswerAnalysis(question.getAnswerAnalysis());
                //添加判断题到题库
                judgeService.insertJudge(judge);
                Judge judgeByTitle = judgeService.selectJudgeByTitle(judge.getTitle());
                TbAll tbAll = new TbAll();
                tbAll.setAllId(judgeByTitle.getId());
                tbAll.setQuBankId(quBankId);
                tbAll.setTitle(judgeByTitle.getTitle());
                tbAll.setType(quTypeId);
                tbAll.setCategoryFirst(judgeByTitle.getCategoryFirst());
                tbAll.setCategorySecond(judgeByTitle.getCategorySecond());
                tbAll.setCategoryThrid(judgeByTitle.getCategoryThrid());
                tbAll.setDegree(judgeByTitle.getDegree());
                //添加题目基本信息到题库和题目的中间表
                tbAllServices.insertTbAll(tbAll);
            }
        })).sheet(sheetNum).doRead();
        TbQuestionSummary questionSummary = new TbQuestionSummary();
        questionSummary.setId(quBankId);
        questionSummary.setJudgeNum(judgeNum.get());
        //更新tb_question_summer表中各种题的数量
        questionSummaryService.updateTbQuestionSummary(questionSummary);
        System.out.println("判断题读取到" + judgeNum.get() + "条数据");
        System.out.println(questionSummary);
    }

    public void readExcelForShortAnswer(String filePath, Long quBankId, Integer quTypeId, int sheetNum){
        AtomicReference<Long> shortAnswerNum = new AtomicReference<>(0L);
        EasyExcel.read(filePath, ExcelShortAnswer.class, new PageReadListener<ExcelShortAnswer>(questionList -> {
            for (ExcelShortAnswer question : questionList) {
                shortAnswerNum.getAndSet(shortAnswerNum.get() + 1);
                System.out.println(question);
                ShortAnswer shortAnswer = new ShortAnswer();
                shortAnswer.setQuBankId(quBankId);
                shortAnswer.setTitle(question.getTitle());
                shortAnswer.setCategoryFirst(question.getCategoryFirst());
                shortAnswer.setCategorySecond(question.getCategorySecond());
                shortAnswer.setCategoryThrid(question.getCategoryThird());
                shortAnswer.setDegree(Integer.parseInt(question.getDegree()));
                shortAnswer.setAnswer(question.getAnswer());
                shortAnswer.setAnswerAnalysis(question.getAnswerAnalysis());
                shortAnswer.setAnalysisImg(question.getAnswerImg());
                shortAnswerService.insertShortAnswer(shortAnswer);
                TbAll tbAll = new TbAll();
                tbAll.setAllId(shortAnswer.getId());
                tbAll.setQuBankId(quBankId);
                tbAll.setTitle(shortAnswer.getTitle());
                tbAll.setType(quTypeId);
                tbAll.setCategoryFirst(shortAnswer.getCategoryFirst());
                tbAll.setCategorySecond(shortAnswer.getCategorySecond());
                tbAll.setCategoryThrid(shortAnswer.getCategoryThrid());
                tbAll.setDegree(shortAnswer.getDegree());
                //添加题目基本信息到题库和题目的中间表
                tbAllServices.insertTbAll(tbAll);
            }
        })).sheet(sheetNum).doRead();
        TbQuestionSummary questionSummary = new TbQuestionSummary();
        questionSummary.setId(quBankId);
        questionSummary.setShortAnswerNum(shortAnswerNum.get());
        //更新tb_question_summer表中各种题的数量
        questionSummaryService.updateTbQuestionSummary(questionSummary);
        System.out.println("简答题读取到" + shortAnswerNum.get() + "条数据");
        System.out.println(questionSummary);
    }
}
